package view.ttph;

import java.io.File;

import javax.faces.event.ActionEvent;

import oracle.adf.model.BindingContext;

import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.view.rich.component.rich.RichPopup;
import oracle.adf.view.rich.component.rich.output.RichOutputText;
import oracle.adf.view.rich.event.DialogEvent;
import oracle.adf.view.rich.event.PopupCanceledEvent;

import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;

import org.apache.myfaces.trinidad.model.UploadedFile;

import view.common.Utils;

public class thongtinphanhoiBean {
    private RichPopup popupThemMoi;
    private UploadedFile _file;
    private RichOutputText inputtextOperator;
    
    public thongtinphanhoiBean() {
    }

    public BindingContainer getBindings() {
        return BindingContext.getCurrent().getCurrentBindingsEntry();
    }

    public void create(ActionEvent actionEvent) {
        Utils.execute("CreateInsert");
        Utils.showPopup(popupThemMoi);
        inputtextOperator.setValue("0");
        
    }

    public void update(ActionEvent actionEvent) {
        Utils.showPopup(popupThemMoi);
        inputtextOperator.setValue("1");
    }
    
    public void commitDialog(DialogEvent dialogEvent) {
        try {
            int operator=inputtextOperator.getValue().equals("1")?1:0;
            Boolean successUploaded = true;
            File filePath =
                new File(Utils.ENUM_CAU_HINH_KHOA.FILE_ROOT.getValue("HtCauHinhView1Iterator"),
                         Utils.ENUM_CAU_HINH_KHOA.PAKN_DIR.getValue("HtCauHinhView1Iterator"));
            
            Object fileId = null;
            DCIteratorBinding detailIterator1 =
                Utils.getIteratorBinding("ThongTinPhanHoiView1Iterator");
            DCIteratorBinding fileIterator1 =
                Utils.getIteratorBinding("HtFileView1Iterator");
            
            if (this.getFile() != null) {
                    if (operator==1) {
                    fileId =
                            detailIterator1.getCurrentRow().getAttribute("FileId");
                    if (fileId == null) {
                        Utils.execute("HtFileInsert");
                        fileId =
                                fileIterator1.getCurrentRow().getAttribute("Id").toString();
                    }
                } else {
                    Utils.execute("HtFileInsert");
                    fileId =
                            fileIterator1.getCurrentRow().getAttribute("Id").toString();
                }
                if (Utils.fileUpload(this.getFile(), filePath, fileId.toString())) {
                    Utils.processUpload(this.getFile(), filePath,
                                        "HtFileView1Iterator",
                                        "ThongTinPhanHoiView1Iterator",
                                        operator,
                                        Utils.ENUM_MA_UNG_DUNG.TTPH, fileId.toString());
                } else {
                    successUploaded = false;
                }
            }

            if (successUploaded) {
                Utils.execute("Commit");
                Utils.iteratorRefresh("ThongTinPhanHoiView1Iterator");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
    }

    public void cancelDiaglog(PopupCanceledEvent popupCanceledEvent) {
        Utils.execute("Rollback");
    }
    
    public void deleteDiaglog(DialogEvent dialogEvent) {
        Utils.execute("Delete");
        Utils.execute("Commit");
    }

    public void setPopupThemMoi(RichPopup popupThemMoi) {
        this.popupThemMoi = popupThemMoi;
    }

    public RichPopup getPopupThemMoi() {
        return popupThemMoi;
    }

    public void setFile(UploadedFile _file) {
        this._file = _file;
    }

    public UploadedFile getFile() {
        return _file;
    }

    public void setInputtextOperator(RichOutputText inputtextOperator) {
        this.inputtextOperator = inputtextOperator;
    }

    public RichOutputText getInputtextOperator() {
        return inputtextOperator;
    }
}
